package data_structure.stack;

import lombok.Data;

import java.util.Arrays;

/**
 * @ClassName : data_structure.stack.ArrayStack
 * @Description : 类描述
 * Created by cyd on 2021-01-31 23:03:09
 */
@Data
public class ArrayStack {
    private Integer maxSize;
    private Integer[] stack;
    private Integer top = -1;

    public ArrayStack(Integer maxSize) {
        this.maxSize = maxSize;
        this.stack = new Integer[maxSize];
    }

    // 判断栈为空
    public boolean isEmpty() {
        return this.top == -1;
    }

    // 判断栈是否已满
    public boolean isFull() {
        return this.top == maxSize - 1;
    }

    // 向栈中添加数据
    public void push(int n) {
        if (this.isFull()) {
            System.out.println("此栈已满，不能压入");
            return;
        }
        top = top + 1;
        stack[top] = n;
    }

    // 向栈中弹出数据
    public Integer pop() {
        if (this.isEmpty()) {
            System.out.println("此栈已空，不能弹出");
            return null;
        }
        Integer value = stack[top];
        stack[top] = null;
        top = top - 1;
        return value;
    }

    // peek方法
    public int peek() {
        return stack[top];
    }

    // 输出栈
    public void show() {
        System.out.println(Arrays.toString(this.stack));
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(3);
        System.out.println("--------------向空栈中弹出数据--------------");
        stack.pop();
        System.out.println("--------向空栈中压入一个数据并展示-----------");
        stack.push(10);
        stack.show();
        stack.push(20);
        stack.push(30);
        System.out.println("--------------向满栈中压入数据--------------");
        stack.push(40);
        stack.show();
        System.out.println("-----------向满栈弹出一个数据并展示----------");
        Integer value = stack.pop();
        System.out.println("弹出的值：" + value);
        stack.show();
    }
}
